<?php

class Chat_topic_m extends MY_Model {

    protected $_table_name = 'tbl_topics';
    protected $_primary_key = 'topic_id';
    protected $_primary_filter = 'intval';
    protected $_order_by = '';

    // pagination 
    public function record_count() {
        return $this->db->count_all($this->_table_name);
    }

    public function fetch_topic($limit, $start) {
        $this->db->limit($limit, $start);
        $query = $this->db->get($this->_table_name);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

    public function count_thread($topic_id) {
        $this->db->where('topic_id', $topic_id);
        $this->db->from('tbl_threads');
        return $this->db->count_all_results();
    }

    public function get_author($user_id) {
        $this->db->where('id', $user_id);
        $this->db->from('user');
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

    public function get_thread($Thread_id) {
        $this->db->where('thread_id', $Thread_id);
        $this->db->from('tbl_threads');
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

    function reply_to($authorID, $thread_id) {
        $data['rep_title'] = $this->input->post('reply_title');
        $data['rep_content'] = $this->input->post('reply_content');
        $data['rep_dated'] = $this->input->post('date');
        $data['rep_author'] = $authorID;
        $data['thread_id'] = $thread_id;
        $data['rep_status'] = $this->input->post('reply_status');

        return $this->db->insert('tbl_replies', $data);
    }

    function getAuthorID($email) {
        $Query = $this->db
                ->from('user')
                ->where('email', $email)
                ->get();
        if ($Query->num_rows() > 0) {
            return $Query->row()->id;
        }
    }

    function add_other($authorID) {
        $data['title'] = $this->input->post('other_title');
        //$data['userId'] = $this->input->post('author');
        $data['userId'] = $authorID;
        $data['body'] = $this->input->post('other_body');
        $data['createdDate'] = $this->input->post('date');

        return $this->db->insert('post', $data);
    }

}